package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.locks.Lock;

import boundary.IMenu;
import data.IData;
import functionality.ITransmitter;

public class MenuController implements IMenuController {

	private IMenu menu;
	private OperatorLoginController oprLogin;
	private ITransmitter transmit;

	private PrintWriter out;
	private BufferedReader in;
	private String host;
	private int port;
	private String[] args;

	public MenuController (IMenu menu, OperatorLoginController oprLogin, ITransmitter transmit, String[] args) {
		this.menu = menu;
		this.oprLogin = oprLogin;
		this.transmit = transmit;
		this.args = args;
		this.host = args[0];
		this.port = Integer.parseInt(args[1]);
	}


	public void run(){
		Thread loginThread = new Thread(oprLogin, "RM20 Tråd");
		loginThread.start();

		// Vi sikrer at operatør-login tråden starter først og låser de synchronized metoder i Transmitter
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		startMenu(); // Resten af programmet startes

		// Vi sikrer at den oprindelige login-tråd er afsluttet før programmet afsluttes
		try {
			loginThread.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void startMenu(){
		try (Socket	socket = new Socket(host, port);
				PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));){	
			this.out = out;
			this.in = in;
			program();
		} catch (UnknownHostException e) {
			System.out.println("Ukendt host: " + host);
			System.exit(1);
		} catch (IOException e) {
			System.out.println("Ingen forbindelse... Genstart program.");
			System.exit(1);
		}	
	}

	@Override
	public void program() throws IOException{
		int choice;
		String reply = "";
		do {
			choice = menu.showMenu();
			switch (choice){
			case 1:
				reply = transmit.readScale(out, in);
				break;
			case 2:
				reply = transmit.tareScale(out, in);
				break;
			case 3:
				reply = transmit.zeroScale(out, in);
				break;
			case 4: {
				Lock lock = transmit.getLock();
				if (lock.tryLock()){
					try{
						String text = menu.getText();
					reply = transmit.dispTxtScl(out, in, text);
					} finally {
						lock.unlock();
					}
				} else {
					reply = "Vægt optaget.";
				}
				
			}
				break;
			case 5:
				reply = transmit.dispWghtScl(out, in);
				break;
			case 6: 
				{
					startOprLogin();
					reply = "Operatør-login forsøgt startet.";
				}
			break;
			case 7:
				reply = "Menuen afsluttes.";
				break;
			default:
				reply = "Input ikke godkendt.";	
			}
			menu.showMsg(reply);
		}
		while (choice != 7);
	}

	@Override
	public void startOprLogin(){
		Thread loginThread = new Thread(new OperatorLoginController(args, transmit));
		loginThread.start();
	}

}
